package com.young.ysepay.service.impl;


import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.young.ysepay.dto.YsPayDTO;
import com.young.ysepay.service.YsBuildRequestParaService;
import com.young.ysepay.service.YsBusinessPramService;
import com.young.ysepay.service.YsPayQueryService;
import com.young.ysepay.service.YsVerifyJsonSignService;
import com.young.ysepay.utils.DateUtil;
import com.young.ysepay.utils.Https;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 银盛支付单查询
 * @Author Vincentxin
 * @Date 2019-06-28
 */
@Service
public class YsPayQueryServiceImpl implements YsPayQueryService {
    public static final Logger LOGGER = LoggerFactory.getLogger(YsPayQueryService.class);
    @Autowired
    private YsBusinessPramService ysBusinessPramService;
    @Autowired
    private YsBuildRequestParaService ysBuildRequestParaService;

    @Autowired
    private YsVerifyJsonSignService ysVerifyJsonSignService;

    /**
     * 通过outTradeNo或者tradeNo 查询
     * @param reqparam
     * @return
     */
    @Override
    public Map<String, Object> ysPayQuery(Map<String,String> reqparam) {
        LOGGER.error("查询银盛支付单"+reqparam.toString());
        String outTradeNo = reqparam.get("outTradeNo");
        String tradeNo = reqparam.get("tradeNo");

        Map<String, Object> realResponse = new HashMap<String, Object>();
        YsPayDTO ysPayDTO = ysBusinessPramService.ysBusinessPram();
        Map<String, String> param = new HashMap<String, String>();
        param.put("method", "ysepay.online.trade.query");
        param.put("partner_id", ysPayDTO.getPARTNER_ID());
        param.put("timestamp", DateUtil.getDateFormat(new Date(), DateUtil.simple));
        param.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        param.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        param.put("version", ysPayDTO.getVERSION());
        Map<String, String> biz_contentMap = new HashMap<String, String>();

        if (StringUtils.isNotEmpty(outTradeNo)){
            biz_contentMap.put("out_trade_no", outTradeNo);
        }else if (StringUtils.isNotEmpty(tradeNo)){
            biz_contentMap.put("trade_no", outTradeNo);
        }else{
            return realResponse;
        }

        param.put("biz_content", JSON.toJSONString(biz_contentMap));
        //签名
        Map<String, String> sPara = ysBuildRequestParaService.buildRequestPara(param);
        try {
            //向银盛发起请求
            String returnString = Https.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL_SEARCH(), sPara);
            //请求回执处理
            //回执参数JSON转map
            Map<String, Object> resultMap = (Map<String, Object>) JSON.parse(returnString);
            Object obj = resultMap.get("ysepay_online_trade_query_response");
            if (obj != null) {
                Map<String, String> ysepay_online_trade_query_response = (Map) JSON.parseObject(obj.toString());
                realResponse.putAll(ysepay_online_trade_query_response);
                int ss = returnString.indexOf("ysepay_online_trade_query_response");
                //截出验签内容
                String str = returnString.substring(ss);
                str = str.replace("ysepay_online_trade_query_response", "");
                str = str.substring(2, str.length() - 1);
                boolean verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), str, "utf-8");
                realResponse.put("verifyResult", verifyResult == true ? "true" : "false");
                return realResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        } finally {
            return realResponse;
        }


    }
}
